<?php
namespace BoxEng\Controller\Wap;
//use Think\Controller;
class IndexController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->nav_cur = "index";
        $this->subtitle = BoxModel("eng_subtitle");
        $this->subtitle_words = BoxModel("eng_subtitle_words");
    }

    function test()
    {
        BoxModel("system_member_uuid")->where("id in (1,2)")->save(array('user_id' => 274));
    }

    public function index()
    {
        $my_read_detail = S($this->user_id . "_my_read_detail");
        //dump($my_read_detail);exit();
        if ($my_read_detail['id'] > 0 && !I('to')) {
            $get = I();
            $array = array('id' => $my_read_detail['id'], 'part_id' => $my_read_detail['part_id']);
            $array = array_merge($array, (array)$get);
            $this->redirect(U('detail', $array));
            exit();
        }
        $this->cur_tabbar_active = "detail";
        $data = $this->subtitle->get("is_show=1", 8, "id desc");
        $this->data = $data['list'];
        $this->show_page($data['pager']);
        $this->display();
    }


    public function detail()
    {

        $this->id = $id = I('id');
        $this->cur_tabbar_active = "detail";
        $this->subData = $this->subtitle->where(array('id' => $id))->find();
        $this->data = $this->subtitle_words->load_words($id);
        $this->part_id = I('part_id', "0", "intval");
        $this->mp4_url = OssDomain . "video/" . $this->subData['path'];
        /*****如果没有id，就读书缓存*/
        if (!$this->id) {
            $read_detail = S($this->user_id . "_my_read_detail");
            $this->id = $read_detail['id'];
            $this->part_id = $read_detail['part_id'];
        }

        $data_json = (array)$this->data[$this->part_id + 1];
        S($this->user_id . "_my_read_detail", array(
            'id' => $this->id,
            'part_id' => $this->part_id,
        ));
        $len = count($this->data);
        for ($i = $len; $i > 0; $i--) {
            $parts[] = array(
                'name' => "第{$i}节",
                'key' => $i - 1,
                'lines' => count($this->data[$i])
            );
        }
        // dump($parts);
        $key = $this->user_id . "_read_words";
        $my_read_words = array(
            'title' => $this->subData['section'],
            'id' => $this->id,
            'path' => $this->mp4_url,
            'parts' => $parts,
        );
        $this->my_read_words = json_encode($my_read_words);
        S($key, $my_read_words);
        $this->data_json = json_encode($data_json);
        $this->display();
    }

    public function notebook()
    {
        $this->cur_tabbar_active = "notebook";
        $this->type = I('type', "new");
        $where = "user_id='$this->user_id'";
        if ($this->type == "all" || $this->type == "complete") {
            $limit = 5;
        }
        if ($this->type == "new") {
            $limit = 300;
            $where .= " and remember=0";
        }
        if ($this->type == "complete") {
            $where .= " and remember=1";
        }


        $this->all_count = BoxModel("eng_notebook")->where("user_id='$this->user_id'")->count();
        $this->new_count = BoxModel("eng_notebook")->where("user_id='$this->user_id' and remember=0")->count();
        $this->complete_count = BoxModel("eng_notebook")->where("user_id='$this->user_id' and remember=1")->count();

        $data = BoxModel("eng_notebook")->get($where, $limit, "addtime desc");
        $list = array();
        foreach ($data['list'] as $k => $one) {
            $word = $one['word'];
            $one2 = (array)$this->get_word($word);
            $list[$k] = array_merge($one, $one2);
        }
        $this->data = $list;
        $this->show_page($data['pager']);
        $this->empty = '<div class="empty">恭喜,您已背完所有新单词！！</div>';
        $this->display();
    }

    public function remember()
    {
        $id = I('id', "0", "intval");
        $r = BoxModel("eng_notebook")->where(array('id' => $id))->save(array('remember' => 1));
        echo $r;
    }


    public function show_word()
    {
        $this->word = $word = strtolower(I("word"));
        $this->data = $this->get_word($word);
        $this->display();
    }

    function get_word($word)
    {
        $data = eng_dictionary($word);
        $data = $data['symbols'][0];
        foreach ($data['parts'] as $k => $one) {
            $data['parts'][$k]['part'] = $one['part'];
            $data['parts'][$k]['means'] = implode("；", $one['means']);
        }
        return $data;
    }

    /****生收藏单词**/
    public function add_favorite()
    {
        $this->word = $word = strtolower(I("word"));
        if (!$this->user_id) {
            echo "请先登录";
            exit();
        }
        if (!$word) return;
        $row = $where = array(
            'user_id' => $this->user_id,
            'word' => $word,
        );
        $r = BoxModel("eng_notebook")->where($where)->find();
        $row['addtime'] = date("Y-m-d H:i:s");
        if ($r) {
            $bool = BoxModel("eng_notebook")->where($where)->save($row);
        } else {
            $bool = BoxModel("eng_notebook")->add($row);
        }
        if ($bool) {
            echo 1;
        } else {
            echo "系统错误";
        }

    }

}

